
/* 

Name: table_OA8.do 
Created: June 2022
Aim: look at RS and the impact of distance on trade flows  

Inputs: 

- upstreamness_hs6.dta 
- baci02_2004.csv 
- country_code_baci02.csv
- dist_cepii.dta 

*/ 

cd $countrypath 

use dist_cepii, clear
sort iso_o iso_d
save, replace

import delimited country_code_baci02.csv, delimiter(comma) clear
keep i iso3 
rename i code
duplicates drop code, force 
sort code 
save temp, replace

import delimited baci02_2004.csv, delimiter(comma) clear
g code= i 
sort code 
merge m:1 code using temp 
keep if _m ==3 
drop _m 
rename iso3 iso_o 
drop code 
g code = j 
sort code 
merge m:1 code using temp 
keep if _m ==3 
drop _m 
drop code
rename iso3 iso_d 
sort iso_o iso_d
merge m:1 iso_o iso_d using dist_cepii
drop _m 
tostring hs6, replace
replace hs6= "0"+ hs6 if length(hs6)==5
drop if length(hs6)<6
rename hs6 hs6_2002
sort hs6 
merge m:1 hs6 using $rspath/rs_hs02_19962006 
keep if _m==3 
drop _merge 
g luv=log(v/q) 
g lv=log(v)
g ldist=log(distw)
egen od=group(iso_o iso_d)
egen ok=group(iso_o hs6) 
egen dk=group(iso_d hs6) 
egen o=group(iso_o) 
egen d=group(iso_d) 
egen k=group(hs6) 

rename hs6_2002 hs6 
sort hs6 
merge m:1 hs6 using $sectorpath/upstreamness_hs6.dta // Antras et al. measure of upstreamness
drop if _m==2 
drop _m 

g dist_up=ldist*upstreamness

local measure rs_hs6_b
g dist_ris=ldist*`measure'

local measure rs_hs6_b
reghdfe lv  `measure'   upstreamness   ldist, a(o d) cluster(od)
outreg2 using $outputpath/table_online_8.xls, replace ctitle(log(exports)) dec(3)

reghdfe lv dist_ris dist_up ldist upstreamness  `measure' , a(o d)  cluster(od)
outreg2 using $outputpath/table_online_8.xls, append ctitle(log(exports)) dec(3)

reghdfe lv dist_ris dist_up ldist upstreamness  `measure' , a(o k d)  cluster(od)
outreg2 using $outputpath/table_online_8.xls, append ctitle(log(exports)) dec(3)

reghdfe lv dist_ris dist_up ldist upstreamness  `measure' , a(ok dk)  cluster(od)
outreg2 using $outputpath/table_online_8.xls, append ctitle(log(exports)) dec(3)
